/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import DAO.DAO_Category;
import DAO.DAO_Manager;
import DAO.DAO_News;
import DAO.DAO_Student;
import Entity.Category;
import Entity.Manager;
import Entity.News;
import Entity.Student;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Hieu Tran
 */
public class API extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        
        
        String apiName = request.getParameter("name");
        if(apiName == null) {
            try (PrintWriter out = response.getWriter()) {
                out.print("hello");
            }
        }
        /**
         * API: list-student
         * Param: name=list-student
         */
        if(apiName.equalsIgnoreCase("list-student")) {
            DAO_Student dao = new DAO_Student();
            List<Student> list = dao.studentsList();
            
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String json = gson.toJson(list);
            
            try (PrintWriter out = response.getWriter()) {
                out.print(json);
            }
            dao.closeConnection();
        }
        
        /**
         * API: login
         * Param: name=login&username=abc&pasword=abc
         */
        if(apiName.equalsIgnoreCase("login")) {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            
            if(username != null && password != null) {
                DAO_Manager daoman = new DAO_Manager();
                Manager person = new Manager(username, password);
                try (PrintWriter out = response.getWriter()) {
                    if (daoman.checkLogin(person)) {
                        out.print("{\"status\":true}");
                    } else {
                        out.print("{\"status\":false}");
                    }
                }
                daoman.closeConnection();
            }
        }
        /**
         * API: list-categories
         * Param: name=list-categories
         */
        if(apiName.equalsIgnoreCase("list-categories")) {
            DAO_Category daocat = new DAO_Category();
            List<Category> list = daocat.categoryList();
            
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String json = gson.toJson(list);
            
            try (PrintWriter out = response.getWriter()) {
                out.print(json);
            }
            daocat.closeConnection();
        }
        
        /**
         * API: list-content-by-cat
         * Param: name=list-content-by-cat&cid=1234
         */
        if(apiName.equalsIgnoreCase("list-content-by-cat")) {
            String cid = request.getParameter("cid");
            if(cid != null) {
                DAO_News daonews = new DAO_News();
                int n = 0;
                try {
                    n = Integer.parseInt(cid);
                } catch (NumberFormatException e) {
                }
                List<News> list = daonews.newsInCat(n);
            
                Gson gson = new GsonBuilder().setPrettyPrinting().create();
                String json = gson.toJson(list);

                try (PrintWriter out = response.getWriter()) {
                    out.print(json);
                }
                daonews.closeConnection();
            }
        }        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
